package cn.analysys.test;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.io.FileNotFoundException;
import java.util.List;

/**
 * 执行流
 * ProcessInstance
 * Execution
 * <p>
 * 启动流程 : (启动后，act_ru_execution表中会存储相关记录)
 * runtimeService.startProcessInstanceById()
 * runtimeService.startProcessInstanceByKey()
 * runtimeService.startProcessInstanceByMessage()
 * <p>
 * 流程参数
 * <p>
 * 流程触发
 * <p>
 * 触发信号事件
 * 捕获事件（Catching）
 * 抛出事件（Throwing）
 */
public class OtherTest4 {

    public static void main(String[] args) throws FileNotFoundException {
        // 启动流程，并查询执行流列表
//        startProcessInstanceById();

        // 启动流程，并查询执行流列表
//        scope();

        // trigger
        trigger();

    }

    /**
     * 启动流程，并查询执行流列表
     */
    public static void startProcessInstanceById() {
        // 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService repositoryService = engine.getRepositoryService();
        // 运行时服务
        RuntimeService runtimeService = engine.getRuntimeService();
        // 部署流程定义
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("datas/var_local.bpmn").deploy();
        // 查询流程定义实体 (查询：act_re_procdef)
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).singleResult();

        // 启动流程后，act_ru_execution表有数据添加
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());

        // 查询act_ru_execution表中数据
        List<Execution> exes = runtimeService.createExecutionQuery().onlyChildExecutions().processDefinitionId(processDefinition.getId()).list();
        System.out.println(exes.size());
    }

    /**
     * 参数范围
     */
    public static void scope() throws FileNotFoundException {
        // 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService repositoryService = engine.getRepositoryService();
        // 创建任务服务
        TaskService taskService = engine.getTaskService();
        // 运行时服务
        RuntimeService runtimeService = engine.getRuntimeService();
        // 部署流程定义
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("datas/scope.bpmn").deploy();
        // 查询流程定义实体 (查询：act_re_procdef)
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).singleResult();

        // 启动流程后，act_ru_execution表有数据添加
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinition.getKey());

        // 得到该流程任务
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
        for (Task task : tasks) {
            System.out.println(task.getName());
            // 得到任务执行流
            Execution execution = runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult();
            if ("TaskA".equals(task.getName())) {
                // 给TaskA设置本地参数
                runtimeService.setVariableLocal(execution.getId(), "taskVarA", "varA");
            } else {
                // 设置全局参数
                runtimeService.setVariable(execution.getId(), "taskVarB", "varB");
            }
        }

        // 将任务A和任务B完成
        for (Task task : tasks) {
            taskService.complete(task.getId());
        }

        // 此时，任务已经走到taskC了
        Task taskc = taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).singleResult();

        // 取出taskA和taskB设置的参数
        System.out.println(runtimeService.getVariable(processInstance.getId(), "taskVarA"));
        System.out.println(runtimeService.getVariable(processInstance.getId(), "taskVarB"));

        System.out.println(processInstance.getId());


    }


    /**
     * 触发流程，让流程向下执行
     */
    public static void trigger() throws FileNotFoundException {
        // 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService repositoryService = engine.getRepositoryService();
        // 创建任务服务
        TaskService taskService = engine.getTaskService();
        // 运行时服务
        RuntimeService runtimeService = engine.getRuntimeService();
        // 部署流程定义
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("datas/receiveTask.bpmn").deploy();
        // 查询流程定义实体 (查询：act_re_procdef)
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).singleResult();

        // 启动流程后，act_ru_execution表有数据添加
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinition.getKey());

        Execution execution = runtimeService.createExecutionQuery()
                .processDefinitionId(processDefinition.getId()).onlyChildExecutions().singleResult();

        System.out.println(processInstance.getId() + ",当前节点 ：" + execution.getActivityId());

        // 触发等待节点
        runtimeService.trigger(execution.getId());

        execution = runtimeService.createExecutionQuery()
                .processDefinitionId(processDefinition.getId()).onlyChildExecutions().singleResult();
        System.out.println(processInstance.getId() + ",当前节点 ：" + execution.getActivityId());
    }
}
